<template>
  <div id="container" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '190px'
    },
    xData: {
      type: Array,
      required: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      var colors = ['#E6A23C', '#178df3', '#409EFF']
      this.chart.setOption({
        color: colors,
        tooltip: {
          trigger: 'axis',
          backgroundColor: '#fff',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '30px',
          containLabel: true
        },
        legend: {
          show: true,
          data: ['月税负', '累计税负', '税负预警线'],
          bottom: '0px',
          align: 'auto'
        },
        xAxis: [{
          type: 'category',
          data: this.xData,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            show: true
          },
          axisLabel: {
            formatter: '{value}%'
          }
        }],
        series: [{
          name: '月税负',
          type: 'bar',
          data: this.chartData.month
        }, {
          name: '累计税负',
          type: 'bar',
          data: this.chartData.total
        }]
      })
    }
  }
}
</script>
